﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace SimpleChatApplication_Client
{
    class Client
    {
        static TcpClient Cliente;
        static NetworkStream NetStream;

        static void Main(string[] args)
        {
            Thread inputthrd = new Thread(new ThreadStart(InputManager));
            inputthrd.Start();
        }

        static void InputManager()
        {
            while (true)
            {
                string command = Console.ReadLine();
                string cmd = command.Split(' ')[0];

                switch (cmd)
                {
                    case "/exit":

                        Disconnect();
                        return;

                    case "/connect":

                        string[] args = command.Split(' ');

                        if ((args.Length == 3) && ((args[1].Length > 1) && (args[2].Length > 1)))
                        {
                            try
                            {
                                string ip = args[1];
                                int port = Convert.ToInt32(args[2]);
                                Cliente = new TcpClient();
                                Cliente.Connect(new IPEndPoint(IPAddress.Parse(ip), port));
                                Console.WriteLine("CLIENT: Client connected on " + ip + ":" + port.ToString());
                                NetStream = Cliente.GetStream();
                                Thread thrdreceieve = new Thread(new ThreadStart(Receieve));
                                thrdreceieve.Start();
                            }
                            catch (Exception ex)
                            {
                                Console.WriteLine("ERROR: " + ex.ToString());
                            }
                        }
                        else
                        {
                            Console.WriteLine("USAGE: /connect [IP] [PORT]");
                        }

                        break;

                    default:

                        if (Cliente.Connected == true)
                        {
                            byte[] datbuf = Encoding.Default.GetBytes(command);
                            SendMessage(datbuf);
                        }

                        break;
                }
            }
        }

        static void Receieve()
        {
            byte[] receive = new byte[64];

            while (true)
            {
                try
                {
                    NetStream.Read(receive, 0, receive.Length);
                    string text = Encoding.Default.GetString(receive);
                    Console.WriteLine("SERVER: " + text);
                }
                catch (Exception ex)
                {
                    Console.WriteLine("ERROR: " + ex.ToString());
                }
            }
        }

        static void SendMessage(byte[] data)
        {
            NetStream.Write(data, 0, data.Length);
            NetStream.Flush();
        }

        static void Disconnect()
        {
            Cliente.Close();
            NetStream.Close();
        }
    }
}
